<template>
  <div>
    <el-button type="text" @click="goBack">返回</el-button>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="标题" prop="title">
        <el-input style="width: 270px" v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-select
          style="width: 270px"
          v-model="ruleForm.categoryId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in category"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-upload
          class="avatar-uploader"
          action="http://81.69.24.232/pet/jz_upload.php"
          :drag="true"
          :show-file-list="false"
          :on-success="succseeHandler"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="ruleForm.cover" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <div slot="tip" class="el-upload__tip">文件大小不允许超过3M</div>
        </el-upload>
      </el-form-item>

      <el-form-item label="正文" prop="content">
        <tinymce v-model="ruleForm.content" :height="240" width="800px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from "@/components/Tinymce";
import { mapState, mapActions, mapGetters } from "vuex";
import { get, post } from "../../utils/request";
export default {
  data() {
    return {
      ruleForm: {},
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        categoryId: [
          { required: true, message: "请输入栏目分类", trigger: "blur" },
        ],
        content: [{ required: true, message: "请输入正文", trigger: "blur" }],
      },
      value: "",
      postForm: {
        content: "",
      },
      disabled: false,
      imageUrl: "",
    };
  },
  components: {
    Tinymce,
  },
  computed: {
    ...mapState("webInfo", ["category", "userInfo"]),
    ...mapGetters(["token"]),
  },
  methods: {
    ...mapActions("webInfo", ["findAllCategory", "getUserInfo"]),
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 限制上传的封面的大小
    beforeAvatarUpload(file) {
      const isLt3M = file.size / 1024 / 1024 < 3;
      if (!isLt3M) {
        this.$message.error("上传头像图片大小不能超过 3MB!");
      }
      return isLt3M;
    },
    // 上传封面
    succseeHandler(file) {
      // file:文件路径
      this.imageUrl = file;
      this.ruleForm.cover = file;
    },
    // 保存信息
    submit() {
      this.ruleForm.authorId = this.userInfo.id;
      post("/article/saveOrUpdate", this.ruleForm).then((res) => {
        this.$router.push("/webInfo/infoMessa");
      });
    },
    // 返回上一页
    goBack() {
      this.$router.go(-1);
    },
  },
  created() {
    this.findAllCategory();
    this.getUserInfo({ token: this.token });
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  line-height: 180px;
  text-align: center;
}
.avatar {
  width: 100%;
  height: 100%;
  display: block;
}
</style>